package simulation;

import javafx.scene.layout.Pane;

import java.util.ArrayList;
import java.util.EnumMap;

/**
 * 主界面类
 */
public class StudentPane extends Pane {

    private final ArrayList<Student> students = new ArrayList<>(1000);

    public StudentPane(int num) {
        setNum(num);

    }

    public void setNum(int num) {
        getChildren().clear();
        for(Space s:Map.getSpaces()){
            s.getStudents().clear();
        }
        students.clear();
        for (int i = 0; i < num; i++) {
            students.add(new Student(0 )); // 三种学生，平均分配
            getChildren().add(students.get(i));
        }
        students.get(0).toInfected();
    }


    /**
     * 重绘函数
     */
    public void repaint() {
        for (Student s : students) {
            s.move();
        }
        for (Space s : Map.getSpaces()) {
            s.infect();
        }
    }

    /**
     * 统计人口函数
     * @return 状态及其人口
     */
    public EnumMap<State, Integer> getCurrPop() {
        EnumMap<State, Integer> currPop = new EnumMap<>(State.class);
        for (Student s : students) {
            if (!currPop.containsKey(s.getState())) {
                currPop.put(s.getState(), 0);
            }
            currPop.put(s.getState(),1 + currPop.get(s.getState()));
        }
        return currPop;
    }
}
